<script src="../service/user.js"></script>
<template>
  <div class="login">
    <s-header :name="type == 'login' ? '登录' : '注册'" :back="'/home'"></s-header>
    <img class="logo" src="../assets/logoshop.jpg" alt="">
    <div v-if="type == 'login'" class="login-body login">
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="username"
          label="账号"
          placeholder="账号"
          :rules="[{ required: true, message: '请填写账号' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />

        <div style="margin: 16px;">
          <div class="link-register" @click="toggle('register')">立即注册</div>
          <van-button round block type="info"  native-type="submit">登录</van-button>
        </div>
      </van-form>
    </div>
    <div v-else class="login-body register">
      <van-form @submit="onSubmit">
        <van-field
          v-model="username1"
          name="username1"
          label="注册邮箱"
          placeholder="请填写注册邮箱"
          :rules="[{ required: true, message: '请填写注册邮箱' }]"
        />
        <van-field
          v-model="password1"
          type="password"
          name="password1"
          label="密码"
          placeholder="请设置密码"

          :rules="[{ required: true, message: '请设置密码' }]"
        >

        </van-field>
        <van-field
          v-model="code"
          name="code"
          placeholder="输入验证码"
          :rules="[{ required: true, message: '请填写验证码' }]"
        >
          <template #button>
            <van-button size="small" color="orangered" type="primary" @click="checkMail()">发送验证码</van-button>
          </template>
        </van-field>

        <div style="margin: 16px;">
          <div class="link-login" @click="toggle('login')">登录已有账号</div>
          <van-button round block type="info" color="#1baeae" native-type="submit">注册</van-button>
        </div>
      </van-form>
      <!--      <van-button round block type="info" color="#1baeae" @click="cc" >check</van-button>-->
    </div>
  </div>
</template>

<script>
  import sHeader from '@/components/SimpleHeader'
  import { login, register, getUserInfo,check } from '@/service/user'
  import { setLocal, getLocal } from '@/common/js/utils'
  import { Toast } from 'vant'
  export default {
    data() {
      return {
        username: '',
        password: '',
        username1: '',
        password1: '',
        type: 'login',
        code:'',
      }
    },
    components: {
      sHeader,
    },
    methods: {
      // cc(){
      //   console.log(JSON.stringify(this.code))
      //   console.log(sessionStorage.getItem("mail"))
      //   console.log(JSON.stringify(this.code)===sessionStorage.getItem("mail"))
      // },
      checkMail() {
        var t  = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/ ;
        console.log(this.username1)
        console.log(t.test(this.username1))
        if (t.test(this.username1)){
          check({"loginName" : this.username1}).then(response=>{
            console.log(response)
            Toast("验证码已发送，请查收")
            sessionStorage.setItem("mail",JSON.stringify( response.data));
          })
        }else {
          Toast("邮箱有误")
        }


      },
      // dealTriVer() {
      //   // 执行验证码的验证，通过 this.verify 知道验证码是否填写正确
      //   this.$refs.loginVerifyRef.$refs.instance.checkCode()
      // },
      toggle(v) {
        this.type = v
      },
      async onSubmit(values) {
        // this.dealTriVer()
        if (this.type == 'login') {
          const { data, resultCode } = await login({
            "loginName": values.username,
            "passwordMd5": this.$md5(values.password)
          }).catch(() => {})
          setLocal('token', data)
          window.location.href = '/'
        } else {
          console.log(JSON.stringify(this.code)===sessionStorage.getItem("mail"))
          if (JSON.stringify(this.code)!==sessionStorage.getItem("mail")) {
            Toast.fail('验证码未填或填写错误!')
            return
          }
          const { data } = await register({
            "loginName": values.username1,
            "password": values.password1
          })
          Toast.success('注册成功')
          this.type = 'login'
        }
      },
      success(obj) {
        // this.verify = true
        // 回调之后，刷新验证码
        obj.refresh()
      },
      error(obj) {
        // this.verify = false
        // 回调之后，刷新验证码
        obj.refresh()
      }
    },
  }
</script>

<style lang="less">
  .login {
    .logo {
      width: 180px;
      height: 155px;
      display: block;
      margin: 80px auto 0px;
    }
    .login-body {
      padding: 0 20px;
    }
    .login {
      .link-register {
        font-size: 14px;
        margin-bottom: 20px;
        color: #1989fa;
        display: inline-block;
      }
    }
    .register {
      .link-login {
        font-size: 14px;
        margin-bottom: 20px;
        color: #1989fa;
        display: inline-block;
      }
    }
    .verify-bar-area {
      margin-top: 24px;
      .verify-left-bar {
        border-color: #1baeae;
      }
      .verify-move-block {
        background-color: #1baeae;
        color: #fff;
      }
    }
    .verify {
      >div {
        width: 100%;
      }
      display: flex;
      justify-content: center;
      .cerify-code-panel {
        margin-top: 16px;
      }
      .verify-code {
        width: 40%!important;
        float: left!important;
      }
      .verify-code-area {
        float: left!important;
        width: 54%!important;
        margin-left: 14px!important;
        .varify-input-code {
          width: 90px;
          height: 38px!important;
          border: 1px solid #e9e9e9;
          padding-left: 10px;
          font-size: 16px;
        }
        .verify-change-area {
          line-height: 44px;
        }
      }
    }
  }
</style>
